//
//  ECPrivateLetterTableViewCell.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/12.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECPrivateLetterTableViewCell.h"
#import "ECPrivateLetterModel.h"
#import "ContactDateHelper.h"

@interface ECPrivateLetterTableViewCell(){
    /// 头像image
    UIImageView *imageView;
    /// 姓名lab
    UILabel *nameLab;
    /// 具体消息lab
    UILabel *detaliLab;
    /// 时间lab
    UILabel *timelab;
    /// 未读数lab
    UILabel *countLab;
}

@end

@implementation ECPrivateLetterTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        
        [self setCellUI];
    }
    return self;
}

-(void)setCellUI{
    
    imageView = [[UIImageView alloc]init];
    [self.contentView addSubview:imageView];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = kProportion_Width(80);
    
    nameLab = [[UILabel alloc]init];
    [self.contentView addSubview:nameLab];
    nameLab.textColor = [UIColor colorWithHexString:@"#333333"];
    nameLab.font = [UIFont systemFontOfSize:17.f];
    
    detaliLab = [[UILabel alloc]init];
    [self.contentView addSubview:detaliLab];
    detaliLab.textColor = [UIColor colorWithHexString:@"#999999"];
    detaliLab.font = [UIFont systemFontOfSize:15.f];
    
    timelab = [[UILabel alloc]init];
    [self.contentView addSubview:timelab];
    timelab.textAlignment = NSTextAlignmentRight;
    timelab.textColor = [UIColor colorWithHexString:@"#999999"];
    timelab.font = [UIFont systemFontOfSize:16.f];
    
    countLab = [[UILabel alloc]init];
    [self.contentView addSubview:countLab];
    countLab.backgroundColor = [UIColor colorWithHexString:@"#FF6701"];
    countLab.layer.masksToBounds = YES;
    countLab.layer.cornerRadius = kProportion_Width(31);
    countLab.textColor = [UIColor colorWithHexString:@"#ffffff"];
    countLab.font = [UIFont systemFontOfSize:16.f];
    countLab.textAlignment = NSTextAlignmentCenter;
    
    UIView *lineView = [[UIView alloc]init];
    [self.contentView addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
    
    WeakSelf(weakSelf);
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView.mas_top).offset(kProportion_Height(37));
        make.left.equalTo(weakSelf.contentView.mas_left).offset(kProportion_Width(45));
        make.width.height.equalTo(@(kProportion_Width(160)));
    }];
    
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView.mas_top).offset(kProportion_Height(60));
        make.left.equalTo(imageView.mas_right).offset(kProportion_Width(30));
        make.width.equalTo(@200);
        make.height.equalTo(@17);
    }];
    
    [detaliLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLab.mas_bottom).offset(kProportion_Height(33));
        make.left.equalTo(nameLab.mas_left);
        make.right.equalTo(countLab.mas_left).offset(-10);
        make.height.equalTo(@16);
    }];
    
    [timelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLab.mas_centerY);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-kProportion_Width(45));
        make.width.equalTo(@80);
        make.height.equalTo(@15);
    }];
    
    [countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(detaliLab.mas_centerY);
        make.right.equalTo(timelab.mas_right);
        make.height.equalTo(@(kProportion_Height(62)));
        make.width.equalTo(countLab.mas_height).multipliedBy(1);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.contentView.mas_bottom);
        make.left.equalTo(imageView.mas_left);
        make.right.equalTo(timelab.mas_right);
        make.height.equalTo(@1);
    }];
}

/*
 cell赋值 
 */
-(void)setContentMode:(ECPrivateLetterModel *)contentMode{
    [imageView sd_setImageWithURL:[NSURL URLWithString:contentMode.smallAvatar] placeholderImage:[UIImage imageNamed:kEduUserAvatarPlaceholderName]];
    nameLab.text = contentMode.nickname;
    detaliLab.text = contentMode.latestMessageContent;
    
    if ([contentMode.unreadNum isEqualToString:@"0"]) {//根据未读个数 显示隐藏数字lab
        countLab.hidden = YES;
    }else{
        countLab.text = contentMode.unreadNum;
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *birthdayDate = [dateFormatter dateFromString:contentMode.latestMessageTime];
    NSString *timeStr = [[ContactDateHelper sharedInstance]stringWithDate:birthdayDate];

    if (timeStr.length == 5) {
        timelab.text = timeStr;
    }else{
        //截取最后时间对应字符串
        NSArray *array = [timeStr componentsSeparatedByString:@" "];
        timelab.text = array[0];
    }
}


@end
